# Load packages ----
#install.packages("pacman")
pacman::p_load(
  dplyr,
  tidyr,
  magrittr,
  sf,
  zoo,
  readxl,
  readr,
  lme4,
  MASS,
  plm,
  fixest,
  foreign,
  Amelia,
  sjmisc,
  snow,
  parallel,
  ggplot2,
  ggpubr,
  gridExtra,
  reshape2,
  xtable,
  marginaleffects,
  margins,
  splines,
  viridis
)


# Source user-written functions ----
## Source functions
funs <- list.files("fun")
sapply(funs, function (file)
  source(paste0("fun/", file)))


# -----------------------------------------------------------------------------


# Computation at the SOEP-FDZ ----
# Software used
# 1. Stata 18.5 MP-Parallel Edition
# 2. R version 4.2.2 (2022-10-31)

# Operating system
# platform       x86_64-suse-linux-gnu       
# arch           x86_64                      
# os             linux-gnu                   
# system         x86_64, linux-gnu

# Session info
# R version 4.2.2 (2022-10-31)
# Platform: x86_64-suse-linux-gnu (64-bit)
# Running under: SUSE Linux Enterprise Server 15 SP5
# 
# Matrix products: default
# BLAS:   /usr/lib64/R/lib/libRblas.so
# LAPACK: /usr/lib64/R/lib/libRlapack.so
# 
# locale:
# [1] LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C               LC_TIME=de_DE.UTF-8        LC_COLLATE=de_DE.UTF-8     LC_MONETARY=de_DE.UTF-8   
# [6] LC_MESSAGES=de_DE.UTF-8    LC_PAPER=de_DE.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
# [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       
# 
# attached base packages:
# [1] splines   parallel  stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] margins_0.3.28         fixest_0.12.1          marginaleffects_0.22.0 xtable_1.8-4           magrittr_2.0.3         reshape2_1.4.4        
# [7] gridExtra_2.3          ggplot2_3.5.1          snow_0.4-4             sjmisc_2.8.10          Amelia_1.8.2           Rcpp_1.0.13           
# [13] foreign_0.8-87         plm_2.6-4              MASS_7.3-60            lme4_1.1-35.5          Matrix_1.6-4           readr_2.1.5           
# [19] readxl_1.4.3           zoo_1.8-12             sf_1.0-17              tidyr_1.3.1            dplyr_1.1.4           
# 
# loaded via a namespace (and not attached):
# [1] Formula_1.2-5       Rdpack_2.6.1        cellranger_1.1.0    yaml_2.3.10         numDeriv_2016.8-1.1 pillar_1.9.0        lattice_0.22-6     
# [8] glue_1.7.0          digest_0.6.37       rbibutils_2.2.16    snakecase_0.11.1    minqa_1.2.8         colorspace_2.1-1    sandwich_3.1-1     
# [15] htmltools_0.5.8.1   plyr_1.8.9          pkgconfig_2.0.3     purrr_1.0.2         mvtnorm_1.3-1       scales_1.3.0        tzdb_0.4.0         
# [22] collapse_2.0.16     emmeans_1.10.4      tibble_3.2.1        proxy_0.4-27        generics_0.1.3      sjlabelled_1.2.0    TH.data_1.1-2      
# [29] pacman_0.5.1        withr_3.0.1         maxLik_1.5-2.1      cli_3.6.3           survival_3.7-0      evaluate_1.0.0      estimability_1.5.1 
# [36] fansi_1.0.6         nlme_3.1-166        class_7.3-22        dreamerr_1.4.0      data.table_1.16.0   tools_4.2.2         hms_1.1.3          
# [43] lifecycle_1.0.4     multcomp_1.4-26     stringr_1.5.1       munsell_0.5.1       prediction_0.3.18   compiler_4.2.2      e1071_1.7-16       
# [50] lfe_3.0-0           rlang_1.1.4         classInt_0.4-10     units_0.8-5         grid_4.2.2          nloptr_2.1.1        rstudioapi_0.16.0  
# [57] miscTools_0.6-28    rmarkdown_2.28      boot_1.3-31         gtable_0.3.5        codetools_0.2-20    DBI_1.2.3           R6_2.5.1           
# [64] knitr_1.48          bdsmatrix_1.3-7     fastmap_1.2.0       utf8_1.2.4          insight_0.20.4      stringmagic_1.1.2   KernSmooth_2.23-24 
# [71] stringi_1.8.4       vctrs_0.6.5         xfun_0.47           tidyselect_1.2.1    coda_0.19-4.1       lmtest_0.9-40    


# Data processing ---- 
# Execute 01-01-process-soep-data.do in Stata
# Execute 01-02-process-soep-microm.do in Stata
source_log("01-03-process-f+b-data.R")


# Data imputation and post-processing ----
source_log("02-01-imputation.R")
source_log("02-02-post-processing.R")


# Descriptives and auxiliary analyses ----
source_log("03-01-correlates-estimation.R")
source_log("03-02-correlates-processing.R")
source_log("03-03-vote-plots.R")
source_log("03-04-subset-descriptives-estimation.R")
source_log("03-05-subset-descriptives-processing.R")


# Main analyses ----
source_log("04-01-main-analyses.R")
source_log("04-02-main-analyses-processing.R")

# Supplementary analyses ---
source_log("05-01-aux-analyses-fe.R")
source_log("05-02-aux-analyses-fe-processing.R")
source_log("05-03-aux-analyses-infladj.R")
source_log("05-04-aux-analyses-infladj-processing.R")
source_log("05-05-aux-analyses-microm.R")
source_log("05-06-aux-analyses-microm-processing.R")
source_log("05-07-aux-mediation.R")
source_log("05-08-aux-sensitivity.R")


# -----------------------------------------------------------------------------


# Computation on a local PC ----
# Software used
#  R version 4.4.1 (2024-06-14 ucrt)

# Operating system
# platform       x86_64-w64-mingw32               
# arch           x86_64                           
# os             mingw32                          
# crt            ucrt                             
# system         x86_64, mingw32  

# Session info
# > sessionInfo()
# R version 4.4.1 (2024-06-14 ucrt)
# Platform: x86_64-w64-mingw32/x64
# Running under: Windows 10 x64 (build 19044)
# 
# Matrix products: default
# 
# 
# locale:
# [1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8   
# [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C                          
# [5] LC_TIME=English_United States.utf8    
# 
# time zone: Europe/Berlin
# tzcode source: internal
# 
# attached base packages:
# [1] splines   parallel  stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] ggpubr_0.6.0           viridis_0.6.5          viridisLite_0.4.2      margins_0.3.28        
# [5] marginaleffects_0.23.0 xtable_1.8-4           reshape2_1.4.4         gridExtra_2.3         
# [9] ggplot2_3.5.1          snow_0.4-4             sjmisc_2.8.10          Amelia_1.8.2          
# [13] Rcpp_1.0.13            foreign_0.8-86         fixest_0.12.1          plm_2.6-4             
# [17] MASS_7.3-60.2          lme4_1.1-35.5          Matrix_1.7-0           readr_2.1.5           
# [21] readxl_1.4.3           zoo_1.8-12             sf_1.0-18              magrittr_2.0.3        
# [25] tidyr_1.3.1            dplyr_1.1.4           
# 
# loaded via a namespace (and not attached):
# [1] tidyselect_1.2.1    sjlabelled_1.2.0    farver_2.1.2        pacman_0.5.1        prediction_0.3.18  
# [6] digest_0.6.37       lifecycle_1.0.4     dreamerr_1.4.0      compiler_4.4.1      rlang_1.1.4        
# [11] tools_4.4.1         utf8_1.2.4          data.table_1.16.2   collapse_2.0.16     ggsignif_0.6.4     
# [16] labeling_0.4.3      bit_4.5.0           classInt_0.4-10     plyr_1.8.9          abind_1.4-8        
# [21] KernSmooth_2.23-24  withr_3.0.1         purrr_1.0.2         numDeriv_2016.8-1.1 grid_4.4.1         
# [26] fansi_1.0.6         e1071_1.7-16        colorspace_2.1-1    scales_1.3.0        insight_0.20.5     
# [31] cli_3.6.3           crayon_1.5.3        miscTools_0.6-28    generics_0.1.3      rstudioapi_0.17.0  
# [36] tzdb_0.4.0          bdsmatrix_1.3-7     minqa_1.2.8         DBI_1.2.3           proxy_0.4-27       
# [41] stringr_1.5.1       cellranger_1.1.0    stringmagic_1.1.2   vctrs_0.6.5         boot_1.3-30        
# [46] sandwich_3.1-1      carData_3.0-5       car_3.1-3           hms_1.1.3           rstatix_0.7.2      
# [51] bit64_4.5.2         Formula_1.2-5       units_0.8-5         glue_1.8.0          nloptr_2.1.1       
# [56] stringi_1.8.4       gtable_0.3.5        lmtest_0.9-40       munsell_0.5.1       tibble_3.2.1       
# [61] pillar_1.9.0        R6_2.5.1            maxLik_1.5-2.1      Rdpack_2.6.1        vroom_1.6.5        
# [66] lattice_0.22-6      backports_1.5.0     rbibutils_2.3       broom_1.0.7         class_7.3-22       
# [71] nlme_3.1-164        mgcv_1.9-1          pkgconfig_2.0.3     

# Descriptives and auxiliary analyses ----
source_log("03-06-market-plots.R")
source_log("03-07-eurostat-plots.R")
source_log("03-08-perceptions-plot.R")

# Post-processing of estimation results ---
source_log("04-03-main-analyses-tables.R")
source_log("05-09-aux-mediation-plots.R")
